package cn.wolfcode.luowowo.redis.service.impl;


import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.redis.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.redis.util.RedisKeys;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserInfoRedisServiceImpl implements IUserInfoRedisService {

    @Autowired
    private StringRedisTemplate template;
    @Override
    public void setVerifyCode(String key, String code, Long time) {
        template.opsForValue().set(key,code,time, TimeUnit.SECONDS);
    }

    @Override
    public String getVerifyCode(String key) {
        return template.opsForValue().get(key);
    }

    @Override
    public void setToken(String key, String user, Long time) {
        template.opsForValue().set(key,user,time,TimeUnit.SECONDS);
    }

    @Override
    public UserInfo getUserInfoByToken(String key) {
        if (key==null){
            return null;
        }
        String userInfo = template.opsForValue().get(key);

        //如果key取出来的值不为空,延长key的时间
        if(userInfo!=null && !userInfo.equals("")){
            template.expire(key,RedisKeys.USER_LOGIN.getTime(),TimeUnit.SECONDS);
        }
        return JSON.parseObject(userInfo,UserInfo.class);
    }

    @Override
    public String getSendSmsFrequency(String frequencyKey) {
        return template.opsForValue().get(frequencyKey);
    }

    @Override
    public void setSendSmsFrequency(String frequencyKey, Long time) {
        //设置这个key的过期时间为一分钟,值随便写无所谓,主要判断key是否过期
        template.opsForValue().set(frequencyKey,"sms",time,TimeUnit.SECONDS);
    }



}
